﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using AdventureWorksModel;
using System.Data.Objects;

/// <summary>
/// Summary description for ProductCategoryDataObject
/// </summary>
[System.ComponentModel.DataObject]
public class ProductCategoryDataObject
{

    public ProductCategoryDataObject()
    {
    }


    public int SelectCount()
    {
        AdventureWorksModel.AdventureWorksEntities entities
            = new AdventureWorksEntities();
        return entities.ProductCategories.Count();
    }


    [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, true)]
    public ObjectQuery<ProductCategory> Select(string sort, int startRowIndex, int maximumRows)
    {
        AdventureWorksModel.AdventureWorksEntities entities
            = new AdventureWorksEntities();

        ObjectQuery<ProductCategory> query = entities.ProductCategories;

        query = query
            .Skip("it.name", "@skip",
                    new ObjectParameter("skip", startRowIndex))
            .Top("@limit", new ObjectParameter("limit", maximumRows));



        return query;
    }

    [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, true)]
    public ObjectQuery<ProductCategory> Select(int startRowIndex, int maximumRows)
    {
        AdventureWorksModel.AdventureWorksEntities entities
            = new AdventureWorksEntities();

        ObjectQuery<ProductCategory> query = entities.ProductCategories
            .Skip("it.name", "@skip",
                    new ObjectParameter("skip", startRowIndex))
            .Top("@limit", new ObjectParameter("limit", maximumRows));

        return query;
    }

    [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Select, true)]
    public System.Data.Objects.IObjectSet<ProductCategory> Select()
    {
        AdventureWorksModel.AdventureWorksEntities entities
            = new AdventureWorksEntities();

        System.Data.Objects.IObjectSet<ProductCategory> set
            = entities.ProductCategories;

        return set;
    }

    [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Delete, true)]
    public void Delete(int categoryId)
    {
        AdventureWorksModel.AdventureWorksEntities entities
            = new AdventureWorksEntities();

        ProductCategory category = entities.ProductCategories.First(c => c.ProductCategoryID == categoryId);
        entities.DeleteObject(category);
        entities.SaveChanges();
    }


    [System.ComponentModel.DataObjectMethod(System.ComponentModel.DataObjectMethodType.Update, true)]
    public void Update(int original_ProductCategoryID, string name)
    {
        AdventureWorksModel.AdventureWorksEntities entities
            = new AdventureWorksEntities();

        ProductCategory category = entities.ProductCategories.First(c => c.ProductCategoryID == original_ProductCategoryID);
        if (category != null)
        {
            category.Name = name;
            entities.SaveChanges();
        }


    }
}